package vn.org.hcmut.or.output;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import vn.org.hcmut.or.model.AvailableJob;
import vn.org.hcmut.or.model.AvailableWindow;
import vn.org.hcmut.or.model.FragmentJob;
import vn.org.hcmut.or.model.SchedulingInfo;
import vn.org.hcmut.or.util.Util;

public class ScheduleOutput {
	SchedulingInfo schedulingInfo;
	public ScheduleOutput(SchedulingInfo schedulingInfo)
	{
		this.schedulingInfo = schedulingInfo;
	}
	public void printScheduleInfo()
	{
//		input
//		4 2
//		9 6 7 10
//		7 15 25

		printScheduleJob();
//		sap xep theo cong viec
//		37 4
//		1 25 9
//		2 7 6
//		3 0 7
//		4 15 10
		
		
//		printScheduleTime();
//		sap xep theo thoi gian bat dau
//		34 4
//		3 0 7
//		2 7 6
//		4 15 10
//		1 25 9
		try {
			BufferedWriter outFileWriter = new BufferedWriter(new FileWriter(".\\output\\output.txt"));
			outFileWriter.write(totalProcess+Util.SPACE+totalFragJob);
			outFileWriter.write(strDetail);
			if (Util.isEndFile) outFileWriter.write(Util.ENDLINE);
			outFileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	int totalProcess = 0;
	int totalFragJob = 0;
	String strDetail="";
	public void printScheduleJob(){
		int indexTotal = 0;
		int totalFragJob = 0;
		ArrayList<Integer> listStartWindow = new ArrayList<Integer>();
		for (AvailableWindow aw:schedulingInfo.getaWindow()){
			listStartWindow.add(aw.getTimeStart());
		}
		for(AvailableJob aj:schedulingInfo.getaJob())
		{
			ArrayList<Integer> list = aj.getDurationJobInWindow();
			for(int k = 0;k<list.size();k++)
			{
				if (list.get(k)>0){
					strDetail+=Util.ENDLINE+(aj.getIdJob()+1)+Util.SPACE+listStartWindow.get(k)+Util.SPACE+list.get(k);
					listStartWindow.set(k, listStartWindow.get(k)+list.get(k));
					indexTotal =Math.max(k,indexTotal);
					totalFragJob++;
				}
					
			}
		}
		this.totalFragJob = totalFragJob;
		this.totalProcess = listStartWindow.get(indexTotal);
		
	}
	
	public void printScheduleTime(){
		int indexTotal = 0;
		int totalFragJob = 0;
		ArrayList<Integer> listStartWindow = new ArrayList<Integer>();
		for (AvailableWindow aw:schedulingInfo.getaWindow()){
			listStartWindow.add(aw.getTimeStart());
		}
		
		for(AvailableWindow aw:schedulingInfo.getaWindow())
		{
			for(AvailableJob aj : this.schedulingInfo.getaJob()){
				int value=aj.getDurationJobInWindow().get(aw.getId());
				if (value>0){
					strDetail+=Util.ENDLINE+(aj.getIdJob()+1)+Util.SPACE+listStartWindow.get(aw.getId())+Util.SPACE+value;
					listStartWindow.set(aw.getId(), listStartWindow.get(aw.getId())+value);
					indexTotal =Math.max(aw.getId(),indexTotal);
					totalFragJob++;
				}
			}
		}
		this.totalFragJob = totalFragJob;
		this.totalProcess = listStartWindow.get(indexTotal);
		
	}
}
